// // ******************************************************************
// //       /\ /|       @file       LocalizationManager.cs
// //       \ V/        @brief      await29
// //       | "")       @author     Await, 285283010@qq.com
// //       /  |
// //      /  \\        @Modified   2024-12-15 09:12
// //    *(__\_\        @Copyright  Copyright (c) , 2024-12-15 09:12 Await
// // ******************************************************************

using System.Reflection;
using AutomaticSubtitles.Contracts.Entity;

namespace AutomaticSubtitles.Service.Infrastructure.Localization;

/// <summary>
/// Class LocalizationManager.
/// </summary>
public class LocalizationManager
{
    private const string CulturesPath = "AutomaticSubtitles.Service.Infrastructure.Localization.iso6392.txt";
    private static readonly Assembly Assembly = typeof(LocalizationManager).Assembly;

    private List<CultureDto> _cultures = new();

    /// <summary>
    /// Loads all resources into memory.
    /// </summary>
    /// <returns><see cref="Task" />.</returns>
    public async Task LoadAll()
    {
        foreach (var resource in Assembly.GetManifestResourceNames())
        {
        }

        await LoadCultures();
    }

    /// <summary>
    /// Gets the cultures.
    /// </summary>
    /// <returns><see cref="IEnumerable{CultureDto}" />.</returns>
    public IEnumerable<CultureDto> GetCultures()
    {
        return _cultures;
    }

    private async Task LoadCultures()
    {
        List<CultureDto> list = [];

        await using var stream = Assembly.GetManifestResourceStream(CulturesPath)
                                 ?? throw new InvalidOperationException($"Invalid resource path: '{CulturesPath}'");
        using var reader = new StreamReader(stream);
        await foreach (var line in reader.ReadAllLinesAsync().ConfigureAwait(false))
        {
            if (string.IsNullOrWhiteSpace(line)) continue;
            var parts = line.Split('|');
            if (parts.Length != 5) continue;
            var name = parts[3];
            if (string.IsNullOrWhiteSpace(name)) continue;
            var twoCharName = parts[2];
            if (string.IsNullOrWhiteSpace(twoCharName)) continue;
            var threeletterNames = string.IsNullOrWhiteSpace(parts[1]) ? new[] { parts[0] } : new[] { parts[0], parts[1] };
            list.Add(new CultureDto(name, name, twoCharName, threeletterNames));
        }

        _cultures = list;
    }

    public static IEnumerable<LocalizationOption<string>> GetLocalizationOptions()
    {
        yield return new LocalizationOption<string>("汉语 (简体字)", "zh");
        // yield return new LocalizationOption<string>("漢語 (繁體字)", "zh-TW");
        // yield return new LocalizationOption<string>("廣東話 (香港)", "zh-HK");
        yield return new LocalizationOption<string>("Afrikaans", "af");
        yield return new LocalizationOption<string>("العربية", "ar");
        yield return new LocalizationOption<string>("Беларуская", "be");
        yield return new LocalizationOption<string>("Български", "bg-BG");
        yield return new LocalizationOption<string>("বাংলা (বাংলাদেশ)", "bn");
        yield return new LocalizationOption<string>("Català", "ca");
        yield return new LocalizationOption<string>("Čeština", "cs");
        yield return new LocalizationOption<string>("Cymraeg", "cy");
        yield return new LocalizationOption<string>("Dansk", "da");
        yield return new LocalizationOption<string>("Deutsch", "de");
        yield return new LocalizationOption<string>("English", "en");
        yield return new LocalizationOption<string>("Ελληνικά", "el");
        yield return new LocalizationOption<string>("Esperanto", "eo");
        yield return new LocalizationOption<string>("Español", "es");
        yield return new LocalizationOption<string>("Eesti", "et");
        yield return new LocalizationOption<string>("Basque", "eu");
        yield return new LocalizationOption<string>("فارسی", "fa");
        yield return new LocalizationOption<string>("Suomi", "fi");
        yield return new LocalizationOption<string>("Filipino", "fil");
        yield return new LocalizationOption<string>("Français", "fr");
        yield return new LocalizationOption<string>("Galego", "gl");
        yield return new LocalizationOption<string>("Schwiizerdütsch", "gsw");
        yield return new LocalizationOption<string>("עִבְרִית", "he");
        yield return new LocalizationOption<string>("हिन्दी", "hi");
        yield return new LocalizationOption<string>("Hrvatski", "hr");
        yield return new LocalizationOption<string>("Magyar", "hu");
        yield return new LocalizationOption<string>("Bahasa Indonesia", "id");
        yield return new LocalizationOption<string>("Íslenska", "is");
        yield return new LocalizationOption<string>("Italiano", "it");
        yield return new LocalizationOption<string>("日本語", "ja");
        yield return new LocalizationOption<string>("Qazaqşa", "kk");
        yield return new LocalizationOption<string>("한국어", "ko");
        yield return new LocalizationOption<string>("Lietuvių", "lt");
        yield return new LocalizationOption<string>("Latviešu", "lv");
        yield return new LocalizationOption<string>("Македонски", "mk");
        yield return new LocalizationOption<string>("മലയാളം", "ml");
        yield return new LocalizationOption<string>("मराठी", "mr");
        yield return new LocalizationOption<string>("Bahasa Melayu", "ms");
        yield return new LocalizationOption<string>("Norsk bokmål", "nb");
        yield return new LocalizationOption<string>("नेपाली", "ne");
        yield return new LocalizationOption<string>("Nederlands", "nl");
        yield return new LocalizationOption<string>("Norsk nynorsk", "nn");
        yield return new LocalizationOption<string>("ਪੰਜਾਬੀ", "pa");
        yield return new LocalizationOption<string>("Polski", "pl");
        yield return new LocalizationOption<string>("Pirate", "pr");
        yield return new LocalizationOption<string>("Português", "pt");
        yield return new LocalizationOption<string>("Românește", "ro");
        yield return new LocalizationOption<string>("Русский", "ru");
        yield return new LocalizationOption<string>("Slovenčina", "sk");
        yield return new LocalizationOption<string>("Slovenščina", "sl-SI");
        yield return new LocalizationOption<string>("Shqip", "sq");
        yield return new LocalizationOption<string>("Српски", "sr");
        yield return new LocalizationOption<string>("Svenska", "sv");
        yield return new LocalizationOption<string>("தமிழ்", "ta");
        yield return new LocalizationOption<string>("తెలుగు", "te");
        yield return new LocalizationOption<string>("ภาษาไทย", "th");
        yield return new LocalizationOption<string>("Türkçe", "tr");
        yield return new LocalizationOption<string>("Українська", "uk");
        yield return new LocalizationOption<string>("اُردُو", "ur_PK");
        yield return new LocalizationOption<string>("Tiếng Việt", "vi");
    }
}